#include "PasswordBox.h"
#include "cocos/editor-support/cocostudio/CocoStudio.h"
//#include "../../Platform/GamePlaza/MissionPlaza.h"
#include "../GameScene/LobbyScene.h"

USING_NS_CC;
using namespace cocostudio;
using namespace ui;

PasswordBox::PasswordBox(void)
{
	m_strCinText = "";
}

PasswordBox::~PasswordBox(void)
{
}

PasswordBox* PasswordBox::create(std::int16_t boxType)
{
	PasswordBox *ret = new PasswordBox();
	if (ret && ret->init(boxType))
	{
		ret->autorelease();
		return ret;
	}
	else
	{
		CC_SAFE_DELETE(ret);
		return nullptr;
	}
}
bool PasswordBox::init(std::int16_t boxType)
{
	Layer::init();
	m_BoxType = boxType;
	auto uiWidget = GUIReader::getInstance()->widgetFromJsonFile("PassWordBox_1/PassWordBox_1.json");
	this->addChild(uiWidget);
	m_btnCancel = dynamic_cast<Button*>(Helper::seekWidgetByName(uiWidget,"btn_cancel"));
	m_btnOK = dynamic_cast<Button*>(Helper::seekWidgetByName(uiWidget,"btn_ok"));
	m_title = dynamic_cast<Text*>(Helper::seekWidgetByName(uiWidget,"txt_boxtitle"));
	m_txtCin = dynamic_cast<TextField*>(Helper::seekWidgetByName(uiWidget,"TextField_3"));
	m_btnCancel->setTag(em_LobbyBTN_pwBox_close);
	m_btnOK->setTag(em_LobbyBTN_pwBox_OK);
	m_txtCin->addEventListenerTextField(this,SEL_TextFieldEvent(&PasswordBox::textFieldCallBack));

	auto listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = [](Touch*,Event*)->bool
	{
		return true;
	};
	listener->setSwallowTouches(true);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,this);

	this->setScale(0);
	this->runAction(Sequence::create(ScaleTo::create(0.3f,1.0),CallFunc::create([&]{
		this->setOpacity(125);
	}),NULL));

	return true;
}
void PasswordBox::SetTitle(char* szTitle)
{
	m_title->setText(szTitle);
}
void PasswordBox::SetBenCallBack(cocos2d::Ref* target,cocos2d::ui::SEL_TouchEvent selector)
{
	m_btnCancel->addTouchEventListener(target,selector);
	m_btnOK->addTouchEventListener(target,selector);
}
void PasswordBox::textFieldCallBack(cocos2d::Ref* sender, cocos2d::ui::TextFiledEventType type)
{
	TextField* txtField = static_cast<TextField*>(sender);
	switch (type)
	{
	case cocos2d::ui::TEXTFIELD_EVENT_ATTACH_WITH_IME:
		if (!strcmp(txtField->getName().c_str(),"TextField_3"))
		{
			m_strCinText = "";
			txtField->setText("");
		}
		break;
	case cocos2d::ui::TEXTFIELD_EVENT_DETACH_WITH_IME:
		break;
	case cocos2d::ui::TEXTFIELD_EVENT_INSERT_TEXT:
		{
			m_strCinText = txtField->getStringValue().c_str();
			txtField->setColor(Color3B::BLACK);
		}
		break;
	case cocos2d::ui::TEXTFIELD_EVENT_DELETE_BACKWARD:
		break;
	default:
		break;
	}
}
